package streaming.test;

import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.SizeChangeEvent;
import javax.media.StartEvent;
import javax.swing.JFrame;

import streaming.MediaCall;

public class Receiver implements ControllerListener{

	
	private Player player, audioPlayer;
	private MediaLocator audioLocator, videoLocator;
	private boolean ready = false;
	private MediaCall call;
	
	public Receiver(MediaCall call, String url){
		this.call = call;
		this.audioLocator = new MediaLocator("rtp://"+url+":"+22222+"/audio");
		this.videoLocator = new MediaLocator("rtp://"+url+":"+22224+"/video");
System.out.println(audioLocator);
System.out.println(videoLocator);
		init();
		
	}

	private synchronized void init() {
		
//		UPnpManager.addUdpForward("RLM", 22222);
//		locator = new MediaLocator("rtp://192.168.1.103:22222/video");
		try {
			player = Manager.createPlayer(videoLocator);
			player.addControllerListener(this);
			
			audioPlayer = Manager.createPlayer(audioLocator);
			audioPlayer.addControllerListener(this);
			
			player.realize();
			wait(5000);
			System.out.println("Realizing video player...");
			
			player.start();
			
			audioPlayer.realize();
			wait(5000);
			System.out.println("Realizing audio player...");
			
			audioPlayer.start();
//			System.out.println("Starting...");
			
		} catch (NoPlayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void controllerUpdate(ControllerEvent e) {
		// TODO Auto-generated method stub
		if ( e instanceof RealizeCompleteEvent){
			System.out.println("Realized");
			setReady(true);
			
//			call.refreshVideoComponent();
			if(false && player.getVisualComponent() != null){
				JFrame frame = new JFrame();
				frame.setBounds(0, 0, 640, 480);
				frame.getContentPane().add("Center", player.getVisualComponent());
				frame.getContentPane().add("South", player.getControlPanelComponent());
				
				frame.addWindowListener(new WindowAdapter(){
					
					public void	windowClosing(WindowEvent e) {
						player.stop();
						player.close();
						System.out.println("Stoped");
						System.exit(0);
					}
				});
				frame.setVisible(true);
			}
			
			notifyAll();
		}
		
		
		if (e instanceof StartEvent){
			
			System.out.println("Stream started");
		}
		else if (e instanceof SizeChangeEvent){
			System.out.println("Size: " + ((SizeChangeEvent)e).getHeight());
		}
	}
	
	public static void main(String[] args) {
	}

	public boolean isReady() {
		return ready;
	}

	public void setReady(boolean ready) {
		this.ready = ready;
	}

	public Component getVisualComponent() {
		
		return player.getVisualComponent();
	}
	
	public void start(){
		player.start();
	}
	
	public void pause(){
		player.stop();
	}
	
	
	public void stop(){
		player.stop();
		player.close();
	}
	
}
